<template>
  <div>
    <Page ref="page" :search-config="searchConfig" :table-config="tableConfig" :toolbar-config="toolbarConfig" />
  </div>
</template>
<script setup lang="ts">
import { getProductSpecInboundOrderPageList, retryInboundOrder } from "@/api/warehouse/productspecinboundorderApi";
import { MPage as Page } from "page-com";
import { PageSearchConfig, PageTableConfig, ToolbarConfig, type PageInstance } from "page-com";
import { reactive, shallowReactive, useTemplateRef } from "vue";
import { hasPerms } from "@/utils/auth";
import use from "@/utils/use";
import Enum from "@/enums/warehouseEnum";
import { getWarehouseSelect } from "@/api/warehouse/warehouseApi";
import { msgConfirm, successMsg } from "@/utils/message";
import { useOffineIcon } from "@/components/ReIcon/src/hooks";
const { gotoPage } = use();
let warehousePromise = getWarehouseSelect().then(v => v.data);
const searchConfig = shallowReactive<PageSearchConfig>({
  pageSearchFrame: [
    {
      frameType: "input",
      key: "orderNo",
      label: "入库单号"
    },
    {
      frameType: "select",
      key: "status",
      options: Enum.ShopProductSpecInboundOrderStatusEnums,
      label: "状态"
    },
    {
      frameType: "datetimerange",
      key: "createTime",
      searchKey: ["createTimeStart", "createTimeEnd"],
      label: "创建时间"
    }
  ],
  searchFun: getProductSpecInboundOrderPageList
});
const tableConfig = reactive<PageTableConfig>({
  pageTableFrame: [
    {
      key: "orderNo",
      label: "入库单号"
    },
    {
      key: "warehouseId",
      label: "仓库",
      render: (data: any, row: any) => {
        return warehousePromise.then((warehouses: SelectVo[]) => {
          const warehouse = warehouses.find((item: any) => item.id === data);
          return warehouse ? warehouse.text : "未知仓库";
        });
      }
    },
    {
      key: "status",
      label: "状态",
      render: (data: any, row: any) => {
        return Enum.ShopProductSpecInboundOrderStatusEnums.find((item: any) => item.id === data)?.text;
      }
    },
    {
      key: "createTime",
      label: "创建时间"
    },
    {
      key: "updateTime",
      label: "修改时间"
    },
    {
      label: "操作",
      frameType: "link",
      value: "重试",
      key: "deleteBtn",
      icon: useOffineIcon("ep:refresh-right"),
      click: showTry,
      visible: (data: ShopProductSpecInboundOrderVo) => {
        return data.status === 2;
      }
    }
  ]
});
const toolbarConfig = shallowReactive<ToolbarConfig>({
  buttons: [
    {
      value: "添加",
      click: showAdd,
      visible: hasPerms("/warehouse/productspecinboundorder/add")
    }
  ]
});
const page = useTemplateRef<PageInstance>("page");
function showAdd() {
  gotoPage("/warehouse/productspecinboundorder/ProductSpecInboundOrderAdd");
}
function showTry(vo: ShopProductSpecInboundOrderVo) {
  msgConfirm("确认重试吗？", async () => {
    let res = await retryInboundOrder(vo.orderNo);
    if (res.code == RESPONSE_CODE.SUCCESS) {
      page.value.onSearch();
    }
    return res;
  });
}
</script>
